<?php

/**
 * Description: 课堂完成下载
 * User: devforma
 * Date: 15/8/11
 * Time: 16:33
 */
class FinishDownload implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (validateUser($queryArgs) !== 1 || empty($queryArgs['course_id']) || empty($queryArgs['class_id'])) {
            return genOutput(1000);
        }

        $room = $cmsdb->getRow('SELECT `id`, `lector_id` FROM `data_live_class` WHERE `id`=:cid', array(':cid' => $queryArgs['class_id']));
        if (empty($room))
            return genOutput(1000);

        //检验有效性
        $exist = $cmsdb->getVar('SELECT `id` FROM `data_liveclass_grade` WHERE `user_id`=:uid AND `class_id`=:cid',
            array(':uid' => $queryArgs['user_id'], ':cid' => $queryArgs['class_id']));

        if (empty($exist)) {
            $cmsdb->insert('data_liveclass_grade', array(
                'course_id' => $queryArgs['course_id'],
                'class_id' => $queryArgs['class_id'],
                'lector_id' => $room->lector_id,
                'user_id' => $queryArgs['user_id'],
                'grade_score' => 0,
                'is_openclass' => 0,
                'enter_time' => date('Y-m-d H:i:s'),
                'comment' => ''
            ));
        }

        return genOutput(1);
    }
}